package com.mmloo.manage.authc;

import com.mmloo.common.exception.MMLOOException;
import com.mmloo.common.model.User;
import com.mmloo.manage.service.LoginManageService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * Created by jessy on 2015/7/12.
 */

@Service
@Transactional
public class MyRealm extends AuthorizingRealm implements Serializable{

    @Autowired
    LoginManageService loginManageService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection){
        String loginName=(String) principalCollection.fromRealm(getName()).iterator().next();
        User user = null;
        try {
            user = loginManageService.findByUsername(loginName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(user!=null){
            SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
            /*info.setRoles(user.getRolesName());
            Collection<Role> roleList=user.getRoles();
            for (Role role : roleList) {
                info.addStringPermissions(role.getPermissionsName());
            }*/

            return info;
        }
        return null;
    }

    /**
     * 身份验证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken authenticationToken) throws AuthenticationException {

        String username = authenticationToken.getPrincipal().toString();
        try {
            User user = loginManageService.findByUsername(username);
            if (null == user)
            {
                throw new UnknownAccountException("用户不存在");
            }
            String principal = username;
            String hashedCredentials = user.getPassword();
            ByteSource credentialsSalt = ByteSource.Util.bytes(user.getUsername() + PasswordHelper.ps);

            String realmName = getName();
            SimpleAuthenticationInfo authentication = new SimpleAuthenticationInfo(principal, hashedCredentials,credentialsSalt,realmName);
            return authentication;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }
}
